<template>
  <div v-if="request"
       element-loading-text="请求中...">
    <div class="request-frame">
      <div class="request-title">
        <span v-if="!isSaved">*</span>
        <span v-if="isEdit">
          <el-input v-model="request.title"
                    style="width:500px;"
                    @blur="titleSave"
                    @change="onChange"></el-input>
        </span>
        <span v-else
              @click="isEdit=true">{{request.title}}</span>
        <div style="float:right;">
          <el-button style="color: mediumseagreen"
                     @click="save()">保存</el-button>
        </div>
      </div>
      <div class="request-toolbar">
        <el-input v-model='request.url'
                  placeholder="请输入内容"
                  class="input-with-select"
                  @change="onChange">
          <el-select v-model="request.method"
                     slot="prepend"
                     style="width: 100px;"
                     @change="onChange">
            <el-option label="GET"
                       :value="0"></el-option>
            <el-option label="POST"
                       :value="1"></el-option>
            <el-option label="PUT"
                       :value="2"></el-option>
            <el-option label="DELETE"
                       :value="3"></el-option>
            <el-option label="OPTIONS"
                       :value="4"></el-option>
          </el-select>
          <el-button type="primary"
                     slot="append"
                     :loading="isRequesting"
                     @click="sendRequest()">发送</el-button>
        </el-input>
      </div>
      <div class="request-main">
        <el-row class="request-main-row">
          <el-col :span="12">
            <div class="request-main-col">
              <el-tabs class="request-tab"
                       value="first">
                <el-tab-pane label="请求头"
                             name="first"
                             :lazy="true">
                  <!-- <el-input type="textarea"
                  :rows="28"
                  v-model="request.header"
                  @change="onChange"/> -->
                  <EditableTable ref="headerTable"
                                 :initParam="initParam"
                                 :initData="request.header" />
                </el-tab-pane>
                <el-tab-pane label="请求体"
                             name="second"
                             :lazy="true">
                  <!-- <el-input type="textarea"
                  :rows="26"
                  v-model="request.body"
                  @change="onChange"/> -->
                  <div>
                    <!-- <MonacoEditor
                        class="editor"
                        ref="requestBodyEditor"
                        v-model="request.body"
                        language="json"
                        @change="onChange"/> -->
                    <MonacoEditor class="editor"
                                  ref="responseBodyEditor"
                                  :value="request.body"
                                  @change="(newValue) => (this.request.body = newValue)" />
                  </div>
                </el-tab-pane>
                <el-tab-pane label="文档"
                             name="third"
                             :lazy="true">
                  <ExampleTable :example="request.requestExample"></ExampleTable>
                </el-tab-pane>
              </el-tabs>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="request-main-col">
              <div class="response-status">
                {{response.statusCode}} {{response.statusText}}
                <!-- <el-select>
                  <el-option value="Json" label="Json"></el-option>
                  <el-option value="Html" label="Html"></el-option>
                </el-select> -->
              </div>
              <el-tabs class="request-tab"
                       value="first">
                <el-tab-pane label="响应体"
                             name="first"
                             :lazy="true">
                  <div>
                    <MonacoEditor class="editor"
                                  ref="responseBodyEditor"
                                  :value="response.body"
                                  :readOnly=true
                                  @change="(newValue) => (this.response.body = newValue)" />
                  </div>
                </el-tab-pane>
                <el-tab-pane label="响应头"
                             name="second"
                             :lazy="true">
                  <HeaderTable :header="response.header"></HeaderTable>
                </el-tab-pane>
                <el-tab-pane label="文档"
                             name="third"
                             :lazy="true">
                  <ExampleTable :example="request.responseExample"></ExampleTable>
                </el-tab-pane>
              </el-tabs>
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
  <div v-else
       style="height: 100%; left: 45%; position: fixed; top: 35%; font-size: 150px; color: #d4d4d4">
    <i class="el-icon-goblet-square-full"></i> Mojito
  </div>
</template>

<style scoped>
.request-title {
  color: #f05654;
  font-weight: 600;
  height: 50px;
  line-height: 50px;
}

.request-toolbar {
  height: 50px;
  line-height: 50px;
}

.request-main {
}

.request-main-row {
  display: flex;
  flex-wrap: wrap;
}

.request-main-row .request-main-col {
  padding: 5px;
}

.response-status {
  height: 40px;
  line-height: 40px;
  color: gray;
  right: 10px;
  position: absolute;
}
.request-tab >>> .el-tabs__header{
  margin-bottom: 5px;
}

.request-tab .el-tabs__content {
  height: 650px;
}


.editor {
  height: 640px;
}
</style>

<script>

import EditableTable from '../components/EditableTable'
import ExampleTable from '../components/ExampleTable'
import HeaderTable from '../components/HeaderTable'
import MonacoEditor from '../components/Monaco'
//import requestModel from '../model/request'
import { getRequest, saveRequest, doRequest } from '../api/request'
import { EventBus } from "@/utils/event-bus";

export default {
  name: 'MainRequest',
  props: [
    'input'
  ],
  data () {
    return {
      isEdit: false,
      isSaved: true,
      isRequesting: false,

      enviroment: '89378C7CCAE94C79BB03488976376448',
      initParam: [{
        prop: "key",
        label: "键"
      },
      {
        prop: "value",
        label: "值"
      }],
      request: {
        id: '',
        title: '',
        status: 0,
        description: '',
        url: '',
        folderId: '',
        body: '',
        header: [],
        method: 0,
        exampleId: '',
        requestExample: { text: '', json: [] },
        responseExample: { text: '', json: [] }
      },
      response: {
        statusCode: 'response',
        statusText: '',
        header: [],
        body: ''
      },
    }
  },
  components: {
    EditableTable,
    ExampleTable,
    HeaderTable,
    MonacoEditor
  },
  created () {
    if (this.input.isNew) {
      //request = new requestModel()
      this.isEdit = true
      this.isSaved = false
      this.request.folderId = this.input.parentId
    } else {
      this.open(this.input.id)
    }
  },
  mounted () {
  },
  methods: {
    init () {
      let that = this
      that.request = null
      that.isEdit = false,
        that.isSaved = true,
        that.isRequesting = false,
        that.response = {
          statusCode: 'response',
          statusText: '',
          header: [],
          body: '',
          example: {
            edit: false,
            text: '',
            json: ''
          }
        }
    },
    titleSave () {
      this.isEdit = false
      if (this.request.title == null || this.request.title === '') {
        this.request.title = "新建接口"
      }
      EventBus.$emit("changeRequestTab", this.request);
    },
    add (parentId) {
      let that = this
      that.init()
      var newRequest = {
        id: '',
        name: '',
        folderId: parentId
      }
      this.isEdit = true
      this.request = newRequest
    },
    open (id) {
      let that = this
      that.init()
      getRequest(id)
        .then(response => {
          that.request = response.data
        })
    },
    sendRequest () {
      let that = this
      that.isRequesting = true
      let _request = that.request
      doRequest({
        Url: _request.url,
        Method: _request.method,
        Header: _request.header,
        Body: _request.body,
        EnviromentId: this.$store.state.app.enviroment.current
      })
        .then(response => {
          var data = response.data
          var _response = that.response
          _response.statusCode = data.statusCode;
          _response.statusText = data.statusText;
          _response.header = data.header;
          _response.body = data.body;
          that.isRequesting = false

          // trigger('editor.action.formatDocument')
          // this.editor.getAction(['editor.action.formatDocument'])._run()
        })
        .catch(response => {
          that.response.body = "请求失败";
          that.isRequesting = false
        })
    },
    save () {
      let that = this
      saveRequest(this.request)
        .then(response => {
          var data = response.data
          that.isEdit = false
          that.isSaved = true
          that.request.id = data.id
          EventBus.$emit("refresh");
        })
        .catch(response => {
        })
    },
    onChange () {
      this.isSaved = false
    }
  }
}
</script>



                        <!-- <div style="height:700px;" v-loading="response.body.loading">
                            <MonacoEditor
                              class="editor"
                              ref="responseBodyEditor"
                              v-model="response.body.text"
                              language="json"/>
                        </div> -->
